/*
 * Alberto Ruiz-Larrea Guillén...11383
 * Erdi Dogruel..................14955
 * Marcos Pérez Martín...........10324
 */

package com.example.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

//This class puts a 1 in selected column of teams table in database to the team you have selected
public class SelectTeam extends AsyncTask<OpenDB, Integer, Long> {

	String team;

	public SelectTeam(String t) {
		team = t;
	}

	@Override
	protected Long doInBackground(OpenDB... params) {

		SQLiteDatabase select = params[0].getWritableDatabase();
		String[] col = { OpenDB.id, OpenDB.name, OpenDB.rating, OpenDB.selected };
		Cursor cursor = select.query(OpenDB.table1, col, null, null, null,
				null, null);
		cursor.moveToFirst();
		ContentValues valores = new ContentValues();
		while (!cursor.isAfterLast()) {
			valores.put(OpenDB.selected, "0");
			select.update("teams", valores, null, null);
			cursor.moveToNext();
		}
		cursor.close();

		String[] args = new String[] { team };
		Cursor c = select.query(OpenDB.table1, col, OpenDB.name + "=?", args,
				null, null, null);
		c.moveToFirst();
		ContentValues values = new ContentValues();
		values.put(OpenDB.selected, "1");
		select.update("teams", values, "name=?", args);
		c.close();
		return null;

	}
}